package org.jeecgframework.web.controller;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.Highchart;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.CbsEsPlanIpServiceI;
import org.jeecgframework.web.system.pojo.base.CbsEsPlanIpEntity;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.ZyCinemaEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: 设备计划巡检
 * @author onlineGenerator
 * @date 2016-10-11 14:53:11
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/cbsEsPlanIpController")
public class CbsEsPlanIpController extends BaseController {

	@Autowired
	private CbsEsPlanIpServiceI cbsEsPlanIpService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 自动生成巡检计划 页面跳转
	 * @return
	 */
	@RequestMapping(params = "cbsEsPlanIp")
	public ModelAndView cbsEsPlanIp(HttpServletRequest request) {
		List<TSDepart> tsDeparts = systemService.findHql("from TSDepart where orgType = ?", StaticValue.DIC_ORG_TYPE_THIRD_PARTY);
		request.setAttribute("tsDeparts", tsDeparts);
		request.setAttribute("year", DateUtils.getYear() + 1);
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsEsPlanIpCalc");
	}

	/**
	 * 实时生成巡检计划调整 页面跳转
	 * @return
	 */
	@RequestMapping(params = "cbsRealTimeGenerateIp")
	public ModelAndView cbsRealTimeGenerateIp(HttpServletRequest request) {
		List<TSDepart> tsDeparts = systemService.findHql("from TSDepart where orgType = ?", StaticValue.DIC_ORG_TYPE_THIRD_PARTY);
		request.setAttribute("tsDeparts", tsDeparts);
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsRealTimeGenerateIp");
	}
	
//	/**
//	 * easyui AJAX请求数据
//	 */
//	@RequestMapping(params = "datagrid")
//	public void datagrid(CbsEsPlanIpEntity cbsEsPlanIp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
//		CriteriaQuery cq = new CriteriaQuery(CbsEsPlanIpEntity.class, dataGrid);
//		cq.getDetachedCriteria().setProjection(Projections.groupProperty(""));
//		//查询条件组装器
//		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsEsPlanIp, request.getParameterMap());
//		try{
//		//自定义追加查询条件
//		}catch (Exception e) {
//			throw new BusinessException(e.getMessage());
//		}
//		cq.add();
//		this.cbsEsPlanIpService.getDataGridReturn(cq, true);
//		TagUtil.datagrid(response, dataGrid);
//	}

	/**
	 * @author xiul
	 * 2016-10-19 上午10:18:41
	 * Description 初始加载 highchart数据 
	 */
	@ResponseBody
	@RequestMapping(params = "getCipChartData")
	public AjaxJson getCipChartData(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			cbsEsPlanIpService.getCipChartData(j,request);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-11 下午3:58:38
	 * Description 计算 设备计划巡检计划 
	 */
	@ResponseBody
	@RequestMapping(params="calcEsPlanIp")
	public AjaxJson calcEsPlanIp(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		try {
			String departId = oConvertUtils.getString(req.getParameter("departId"));
			j.setMsg("计算完毕");
			cbsEsPlanIpService.calcEsPlanIp(j,departId);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("查询异常");
		}
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-11 下午3:59:09
	 * Description 判断是否可以生成设备计划巡检计划
	 */
	@ResponseBody
	@RequestMapping(params="judgeCanGenerateEsIp")
	public AjaxJson judgeCanGenerateEsIp(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		try {
			cbsEsPlanIpService.judgeCanGenerateEsIp(req, j);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("查询异常");
		}
		return j;
	}
	
	/**
	 * @author xiul
	 * 2016-10-26 下午3:59:49
	 * Description 根据已有的数据生成巡检计划 
	 */
	@ResponseBody
	@RequestMapping(params="genarateIp")
	public AjaxJson genarateIp(HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		try {
			cbsEsPlanIpService.genarateIp(j,request);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("查询异常");
		}
		return j;
	}
	
	
	/**
	 * @author xiul
	 * 2016-10-18 下午8:16:39
	 * Description 生成巡检计划页 - 点击查看 （跳转list页）
	 */
	@RequestMapping(params = "goViewList")
	public ModelAndView goViewList(CbsEsPlanIpEntity cbsEsPlanIp, HttpServletRequest req) {
		req.setAttribute("source", req.getParameter("generateSource"));
		req.setAttribute("departId", req.getParameter("departId"));
		req.setAttribute("year", req.getParameter("year"));
		
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsEsPlanIpViewList");
	}
	
	/**
	 * @author xiul
	 * 2016-10-18 下午8:25:00
	 * Description 生成巡检计划页 - 点击查看 datagrid 
	 */
	@RequestMapping(params = "datagridOfViewList")
	public void datagridOfViewList(CbsEsPlanIpEntity cbsEsPlanIp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		try{
			cbsEsPlanIpService.queryDatasForDatagridOfView(request, dataGrid);
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-10-24 下午5:07:45
	 * Description  生成巡检计划页 - 点击查看 列表页 弹出式选择巡检单位 查询条件 
	 */
	@RequestMapping(params="goQueryIpCpForEsPlanIp")
	public ModelAndView goQueryIpCpForEsPlanIp(HttpServletRequest req){
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsQueryIpCpForEsPlanIp");
	}
	
	/**
	 * @author xiul
	 * 2016-10-24 下午5:11:26
	 * Description  生成巡检计划页 - 点击查看 列表页 弹出式选择巡检单位 datagrid 
	 */
	@RequestMapping(params="doCbsQueryIpCpForEsPlanIpData")
	public void doCbsQueryIpCpForEsPlanIpData(TSDepart tsDepart,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){
		CriteriaQuery cq = new CriteriaQuery(TSDepart.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tsDepart, request.getParameterMap());
		try{
			cq.add(Restrictions.eq("orgType", StaticValue.DIC_ORG_TYPE_THIRD_PARTY));//只查询 是第三方的机构
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-10-24 下午5:07:45
	 * Description  生成巡检计划页 - 点击查看 列表页 弹出式选择影院 查询条件 
	 */
	@RequestMapping(params="goQueryCmForEsPlanIp")
	public ModelAndView goQueryCmForEsPlanIp(HttpServletRequest req){
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsQueryCmForEsPlanIp");
	}
	
	/**
	 * @author xiul
	 * 2016-10-24 下午5:11:26
	 * Description  生成巡检计划页 - 点击查看 列表页 弹出式选择影院 datagrid 
	 */
	@RequestMapping(params="doCbsQueryCmForEsPlanIpData")
	public void doCbsQueryCmForEsPlanIpData(ZyCinemaEntity zyCinema,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){
		CriteriaQuery cq = new CriteriaQuery(ZyCinemaEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyCinema, request.getParameterMap());
		try{
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-10-19 下午4:50:04
	 * Description 列表页点击查看 
	 */
	@RequestMapping(params = "goView")
	public ModelAndView goView(HttpServletRequest req) {
		req.setAttribute("cmId", oConvertUtils.getString(req.getParameter("cmId")));
		req.setAttribute("ipYear", oConvertUtils.getString(req.getParameter("ipYear")));
		req.setAttribute("ipMonth", oConvertUtils.getString(req.getParameter("ipMonth")));
		req.setAttribute("source", oConvertUtils.getString(req.getParameter("source")));
		req.setAttribute("departId", oConvertUtils.getString(req.getParameter("departId")));
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsEsPlanIp-view");
	}
	
	/**
	 * @author xiul
	 * 2016-10-19 下午5:22:46
	 * Description 查看页 datagrid 
	 */
	@RequestMapping(params = "datagridOfView")
	public void datagridOfView(CbsEsPlanIpEntity cbsEsPlanIp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		try{
			cbsEsPlanIpService.queryViewDetialData(request, dataGrid);
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-10-19 下午6:07:58
	 * Description 修改当前巡检月份当前影院 的 巡检月份 或 巡检单位 
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(HttpServletRequest req) {
		cbsEsPlanIpService.getUpdateData(req);
		return new ModelAndView("com/zybk/cbs_es_plan_ip/cbsEsPlanIp-update");
	}
	
	/**
	 * @author xiul
	 * 2016-10-26 下午2:43:50
	 * Description 修改 设备的 计划月份 或 巡检服务商 
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doAdd(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "设备计划巡检修改成功";
		try{
			cbsEsPlanIpService.updateCip(j,request);
		}catch(Exception e){
			e.printStackTrace();
			message = "设备计划巡检修改失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * @author xiul
	 * 2016-12-1 下午6:43:47
	 * Description 首页 - 巡检情况图表 堆叠柱状图 
	 */
	@ResponseBody
	@RequestMapping(params="queryHomePageEsIpHighchartData")
	public List<Highchart> queryHomePageEsIpHighchartData(HttpServletRequest request){
		return cbsEsPlanIpService.queryHomePageEsIpHighchartData(request);
	}
	
	/**
	 * @author xiul
	 * 2016-11-16 下午2:39:48
	 * 导出excel 
	 */
	@ResponseBody
	@RequestMapping(params="exportCips")
	public AjaxJson exportCips(HttpServletRequest request, HttpServletResponse response){
		AjaxJson j = new AjaxJson();
		cbsEsPlanIpService.exportCips(request, response,j);
		return j;
	}
	 
}

